//
//  UIButtonFactory.m
//  ZouXiu
//
//  Created by zhou huajian on 11-2-1.
//  Copyright 2011 itotemstudio. All rights reserved.
//

#import "UIButtonFactory.h"
#import "UIUtil.h"

@implementation UIButtonFactory

+ (UIButton *)buttonWithType:(UIButtonType)type title:(NSString *)title titleFont:(UIFont*)font frame:(CGRect)frame
                   imageName:(NSString *)imageName tappedImageName:(NSString *)tappedImageName
                      target:(id)target action:(SEL)selector tag:(NSInteger)tag {
    
	UIButton *button = [UIButton buttonWithType:type];
	button.frame = frame;
	button.tag = tag;
    if (imageName) {
        //CGSize sz = [[UIImage imageNamed:imageName] size];
        UIImage *bg = [UIImage imageNamed:imageName];
        
     	[button setBackgroundImage:[bg stretchableImageWithLeftCapWidth:bg.size.width/2 topCapHeight:0.0] forState:UIControlStateNormal];
        
        
    }
    if (tappedImageName && ![tappedImageName isEqualToString:imageName]) {
     	[button setBackgroundImage:[UIImage imageNamed:tappedImageName] forState:UIControlStateHighlighted];
    }
	if (title) {
        if(font){
            button.titleLabel.font           = font;
        }else{
            button.titleLabel.font            = [UIFont boldSystemFontOfSize: 13];
        }
        if([imageName isEqualToString :@"btn_back_blue"]){
            
            if(title.length == 2)
                button.titleEdgeInsets = UIEdgeInsetsMake(0,6,0,0);
            else{
                button.titleEdgeInsets = UIEdgeInsetsMake(0,1,0,0);
            }
            
        }
        
        button.titleLabel.shadowOffset    = CGSizeMake (0.0, -1.0);
        //    [button setTitleShadowColor:forState:UIControlStateNormal];
        [button setTitleShadowColor:RGBCOLOR(15, 61, 97) forState:UIControlStateDisabled];
		[button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:RGBCOLOR(186, 215, 238) forState:UIControlStateDisabled];
        
	}
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    
    
	return button;
}

+ (ISTThemeButton *)buttonWithType:(UIButtonType)type title:(NSString *)title frame:(CGRect)frame
                         imageName:(NSString *)imageName tappedImageName:(NSString *)tappedImageName
                            target:(id)target action:(SEL)selector tag:(NSInteger)tag{
    //	UIButton *button = [UIButton buttonWithType:type];
    //	button.frame = frame;
    //	button.tag = tag;
    //    if (imageName) {
    //     	[button setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
    //    }
    //    if (tappedImageName && ![tappedImageName isEqualToString:imageName]) {
    //     	[button setImage:[UIImage imageNamed:tappedImageName] forState:UIControlStateHighlighted];
    //    }
    //	if (title) {
    //		[button setTitle:title forState:UIControlStateNormal];
    //	}
    //    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
	ISTThemeButton *button = [[ISTThemeButton alloc] initWithFrame:CGRectZero];
	button.frame = frame;
	button.tag = tag;
    if (imageName) {
        //CGSize sz = [[UIImage imageNamed:imageName] size];
     	[button setBackgroundImageStr:imageName forState:UIControlStateNormal];
    }
    if (tappedImageName && ![tappedImageName isEqualToString:imageName]) {
     	[button setBackgroundImageStr:tappedImageName forState:UIControlStateHighlighted];
        [button setBackgroundImageStr:tappedImageName forState:UIControlStateSelected];
    }
	if (title!=nil) {
        //      button.titleLabel.font            = [UIFont boldSystemFontOfSize: 14];
        //      button.titleLabel.shadowOffset    = CGSizeMake (0.0, -1.0);
		[button setTitle:title forState:UIControlStateNormal];
        [button setTitleColor:DisableTextColor forState:UIControlStateDisabled];
        
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button setTitleColor:RGBCOLORWITH16COLOR(0xfd8584) forState:UIControlStateHighlighted];
        
        [button setTitleColor:RGBCOLORWITH16COLOR(0xfd8584) forState:UIControlStateSelected];
        
        
        
	}
    [button addTarget:target action:selector forControlEvents:UIControlEventTouchUpInside];
    
    
	return button;
}
+ (UIBarButtonItem*) createBarButtonItem: (NSString*) title
                                iconName:(NSString *)iconName
                                   width:(CGFloat)width
                                  target:(id)target
                                  action:(SEL)selector
                                     tag:(NSInteger)tag
                             destructive:(BOOL)destructive

{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //  UIButton* allButton = [[UIButton alloc]init];
    
    //button.titleLabel.text = title;
    if(destructive){
        [button setBackgroundImage:[UIImage imageNamed:@"btn_bar_bg_red"]
                          forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    }
    else{
        [button setBackgroundImage:[UIImage imageNamed:@"btn_bar_bg_white"]
                          forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
    }
    [button setImage:[UIImage imageNamed:iconName]  forState:UIControlStateNormal];
    
    button.frame = CGRectMake(0, 0, width,30);
    
    
    button.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    button.titleLabel.backgroundColor = [UIColor clearColor];
    //    button.titleLabel.textAlignment = UITextAlignmentCenter;
    //    button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    //	button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.shadowColor = [UIColor darkGrayColor];
    button.titleLabel.shadowOffset = CGSizeMake(-0.5,-0.5);
    [button.titleLabel setNumberOfLines:0];
    
    button.imageEdgeInsets = UIEdgeInsetsMake(0.0,-15.0,0.0,0.0);
    button.titleEdgeInsets = UIEdgeInsetsMake(0.0,5.0,0.0,0.0);
    
    
    button.tag = tag;
    
    
    
    [button addTarget:target
               action:selector
     forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem* barItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    // [button release];
    
    return barItem;
}


+ (UIBarButtonItem*) createBarButtonItemWithImage: (NSString*) title
                                         iconName:(NSString *)iconName
                                     disabledIcon:(NSString*)disabledIconName
                                      hilightIcon:(NSString*)hightIconName
                                            width:(CGFloat)width
                                           target:(id)target
                                           action:(SEL)selector
                                              tag:(NSInteger)tag
                                  backgroundImage:(NSString*)backgrdImage
                          backgroundImageDisabled:(NSString*)backgrdDisabledImage
                         backgroundImageHilighted:(NSString*)backgrdHilightedImage
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //  UIButton* allButton = [[UIButton alloc]init];
    
    UIImage* image = [UIImage imageNamed:backgrdImage];
    //button.titleLabel.text = title;
    
    [button setBackgroundImage:image forState:UIControlStateNormal];
    
    [button setBackgroundImage:[UIImage imageNamed:backgrdDisabledImage] forState:UIControlStateDisabled];
    
    [button setBackgroundImage:[UIImage imageNamed:backgrdHilightedImage] forState:UIControlStateHighlighted];
    
    
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    [button setImage:[UIImage imageNamed:iconName]  forState:UIControlStateNormal];
    
    [button setImage:[UIImage imageNamed:disabledIconName] forState:UIControlStateDisabled];
    [button setImage:[UIImage imageNamed:hightIconName] forState:UIControlStateHighlighted];
    
    CGRect rect =  CGRectMake(0, 0, width,30);
    if(width<0)
        rect.size = image.size;
    button.frame =rect;
    
    
    // button.titleLabel.font = [UIFont boldSystemFontOfSize:12];
    button.titleLabel.backgroundColor = [UIColor clearColor];
    //    button.titleLabel.textAlignment = UITextAlignmentCenter;
    //    button.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    //	button.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    //  [button setTitle:title forState:UIControlStateNormal];
    button.titleLabel.shadowColor = [UIColor whiteColor];
    button.titleLabel.font =[UIFont boldSystemFontOfSize:14];
    button.titleLabel.shadowOffset = CGSizeMake (0.0, 1.0);;
    //  [button.titleLabel setNumberOfLines:0];
    
    button.imageEdgeInsets = UIEdgeInsetsMake(0.0,0.0,0.0,0.0);
    button.titleEdgeInsets = UIEdgeInsetsMake(0.0,5.0,0.0,0.0);
    
    
    button.tag = tag;
    
    [button setTitle:title forState:UIControlStateDisabled];
    [button setTitle:title forState:UIControlStateNormal];
    
    
    [button addTarget:target
               action:selector
     forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem* barItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    // [button release];
    
    return barItem;
}


@end
